var countingTaskTypes = {
    'ArrangeTeam':1,
    'Business':1,
    'EnrollRole':1,
    'ExchangeMaterial':1,
    'FightPVE':1,
    'FightPVETo':1,
    'FightRank':1,
    'FightTower':1,
    'GetLevelReward':1,
    'HarvestFood':1,
    'HarvestGold':1,
    'KillGeneral':1,
    'MakeEquip':1,
    'MakeWeaponTo':1,
    'MakeArmorTo':1,
    'MakeAccessoryTo':1,
    'OccupyMine':1,
    'RadePVE':1,
    'RefineRole':1,
    'StrengthEquip':1,
    'TrainRole':1,
    'UpgradeGrouth':1,
    'UpgradeSkill':1,
    'UpgradeSoldier':1,
    'UpgradeTech':1,
    'WearEquip':1,
    'WinPVE':1,
    'UpgradeSoulLevel':1,
};

exports.get_reward = function(player, req, resp, onHandled) {
    var lastScore = 0;
    do {
        if( isNaN(req.args.score) || !gConfTaskReward[req.args.score] ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }
        var score = +req.args.score;

        var user = player.user;
        lastScore = user.mark.daily_task;
        if( lastScore >= score ) {
            resp.code = 1; resp.desc = 'reward got'; break;
        }

        if( player.getDailyTaskScore() < score ) {
            resp.code = 1; resp.desc = 'not reach score'; break;
        }

        var rewardConf = gConfTaskReward[score];
        if( !rewardConf ) {
            resp.code = 1; resp.desc = 'score error'; break;
        }

        var addGold = rewardConf.Gold;
        var addHonor = rewardConf.Honor;

        user.status.gold += addGold;
        user.status.honor += addHonor;

        //获取奖励
        var award = rewardConf.Award1;
        if( award ) {
            var awards = [];
            awards.push(award);
            awards = player.addAwards(awards); 
            resp.data.awards = awards;
        }

        player.markDirty('status.gold');
        player.markDirty('status.honor');
        
        resp.data.gold = addGold;
        resp.data.honor = addHonor;

        user.mark.daily_task = score;
        player.markDirty('mark.daily_task');
        
    }while(false);

    if( (0 == resp.code) && (user.status.level >= 20) ) {
        var diffScore = score - lastScore;
        var addContribute =  diffScore * gConfGlobal.LeagueContributePerActivity;
        var addXp = diffScore * gConfGlobal.LeagueXpPerActivity;
        var worldReq = {
            uid: req.uid,
            mod: 'league',
            act: 'upgrade_league',
            args: {
                contribute : addContribute,
                xp : addXp,
            }
        };

        var worldResp = { };
        requestWorld(worldReq, worldResp, function() {});
        user.league.contribute += addContribute;
        player.markDirty('league.contribute');
    }
    onHandled();
}

exports.get = function(player, req, resp, onHandled) {

    var user = player.user;
    var status = user.status;
    var tasks = [];

    if( user.task.main.id ) {
        tasks.push(user.task.main); 
    }
    
    /*
    if( user.task.branch.id ) {
        tasks.push(user.task.branch);
    }
    */
    
    for( var i=0,max=tasks.length; i<max; i++ ) {
        var task = tasks[i];
        var taskConf = gConfTask[task.id];
        if( !taskConf ) {
            continue; 
        }

        for( var j=1; j<=2; j++ ) {
            var taskType = taskConf['TaskType'+j];
            var taskArg = taskConf['TaskArg'+j];
            var target = taskConf['TaskTarget'+j];
            
            if( !taskType ) {
                continue; 
            }
            
            // 更新非累计型任务的progress
            if( !(taskType in countingTaskTypes) ) {
                if( 'LevelUp' == taskType ) {
                    task.progress[j] = status.level;
                     
                }else if( 'HaveGold' == taskType ) {
                    task.progress[j] = status.gold;

                }else if( 'HaveHonor' == taskType) {
                    task.progress[j] = status.honor;

                }else if( 'HaveStrengthEquip' == taskType ) {
                    var num = 0;
                    for( var id in user.equip ) {
                        if( taskArg <= user.equip[id].strength ) {
                            num += 1;
                        }
                    }
                    
                   task.progress[j] = num;

                }else if( 'HaveGrouthRole' == taskType ) {
                    var num = 0;
                    for( var id in user.role ) {
                        if( taskArg <= user.role[id].grouth ) {
                            num += 1;
                        }
                    }

                    task.progress[j] = num; 

                }else if( 'HaveMakeEquip' == taskType ) {
                    var num = 0;
                    for( var id in user.equip ) {
                        if( taskArg <= user.equip[id].level ) {
                            num += 1;
                        }
                    }
                    
                    task.progress[j] = num;
                }else if( 'HaveMaterial' == taskType ) {
                    task.progress[j] = player.getInventory('material', taskArg);
                }else if( 'UpgradeFightForceTo' == taskType ) {
                    task.progress[j] = player.getFightForce();
                }else if( 'HaveRole' == taskType ) {
                    var num = 0;
                    for( var id in user.role ) {
                        if( taskArg == gConfRole[id].Quantity ) {
                            num += 1; 
                        } 
                    }

                    task.progress[j] = num; 
                }else if( 'UpgradeTechTo' == taskType ) {
                    var num = 0;
                    var tech = user.tech;
                    for( var id in tech ) {
                        if( tech[id] >= taskArg ) {
                            num += 1; 
                        } 
                    }       

                    task.progress[j] = num; 
                }else if( 'HaveArmyRole' == taskType ) {
                    var num = 0;
                    var roles = user.role;
                    for( var id in roles ) {
                        if( roles[id].soldier_level >= taskArg ) {
                            num += 1; 
                        } 
                    }

                    task.progress[j] = num; 
                }else if( 'FightTowerTo' == taskType ) {
                    task.progress[j] = user.tower.highest[1];   
                }else if( 'KillGeneralTo' == taskType ) {
                    if( user.kill.progress - 1 >= taskArg ) {
                        task.progress[j] = 1; 
                    }
                }

            }
        }
    }         

    resp.data.task = {'main': user.task.main};
    onHandled();
}
